﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SitePages_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int auxId = Convert.ToInt32(HttpContext.Current.Request.QueryString["clientID"]);
        int itemId = Convert.ToInt32(HttpContext.Current.Request.QueryString["itemID"]);
        WebServiceAgent userAgent = (WebServiceAgent)Session[Constants.USER_WEB_SERVICE_CONN];
        ERPSIA.AuxiliarAccount client = FinderManager.GetClient(auxId, userAgent.GetClients());
        ERPSIA.Item item = FinderManager.GetItem(itemId, userAgent.GetAvailableItems());
        ItemLabel.Text = "Artículo: " + item.Descripción;
        ClientLabel.Text = "Cliente: " + client.OwnerName;
        loadReportTable(client, item, userAgent.GetSaleHistory(auxId, itemId));
    }

    /// <summary>
    /// Carga la tabla de reportes de cliente por artículo o artículo por cliente
    /// </summary>
    /// <param name="auxId">Id del cliente para el que se quiere reporte. Si no se quiere este filtro enviar un -1</param>
    /// <param name="itemId">Id del artículo para el que se quiere reporte. Si no se quiere este filtro enviar un -1</param>
    private void loadReportTable(ERPSIA.AuxiliarAccount client, ERPSIA.Item item, ERPSIA.SaleReportHistoryDetail[] history)
    {
        foreach (ERPSIA.SaleReportHistoryDetail detail in history)
        {
            TableRow row = new TableRow();
            TableCell clientCell = new TableCell();
            clientCell.Text = client.OwnerName;
            TableCell descriptionCell = new TableCell();
            descriptionCell.Text = item.Descripción;
            TableCell lastSaleCell = new TableCell();
            lastSaleCell.Text = detail.SaleDate.ToString();
            TableCell quantityCell = new TableCell();
            quantityCell.Text = detail.Quantity.ToString("0.00") + " " + item.MeasureAcronim;
            TableCell unitCostCell = new TableCell();
            unitCostCell.Text = "₡ " + detail.UnitCost.ToString("0.00");
            TableCell soldTotalCell = new TableCell();
            soldTotalCell.Text = "₡ " + (detail.UnitCost*detail.Quantity).ToString("0.00");

            row.Cells.Add(clientCell);
            row.Cells.Add(descriptionCell);
            row.Cells.Add(lastSaleCell);
            row.Cells.Add(quantityCell);
            row.Cells.Add(unitCostCell);
            row.Cells.Add(soldTotalCell);
            ItemTable.Rows.Add(row);
            foreach (TableCell cell in row.Cells)
                cell.HorizontalAlign = HorizontalAlign.Center;
        }
    }
}